define([
    "esri/layers/VectorTileLayer"
], function(VectorTileLayer) {
    var vectorTileLayer = VectorTileLayer.createSubclass({
        setData: function(mapView, layer) {
            var me = this;
            if (!mapView) {
                return
            };
            // if (!mapUtils) {
            //     return;
            // }
            // ;
            me._mapView = mapView;
            me._mapUtils = mapView.mapUtils;

            var options = layer.layer || {};
            me._options = options;
            var data = options.data || [];

            if (me._clickHandle && me._clickHandle.remove) {
                me._clickHandle.remove();
                me._clickHandle = null;
            };

            if (options.onClick) {
                var view = mapView.getView();
                me._clickHandle = view.on('click', function(e) {
                    if (me.visible) {
                        me.clickEvent(e);
                    };
                });
                view._vecClickHandleObj = view._vecClickHandleObj || {};
                view._vecClickEvtObj = view._vecClickEvtObj || {};
                view._vecClickHandleObj[me.id] = me._clickHandle;
            }
        },
        clickEvent: function(e) {
            var me = this;
            var view = me.view;
            var pt = me._mapUtils.createPoint(me._mapView, { x: e.mapPoint.x, y: e.mapPoint.y });
            e.getLocation = function() {
                return me.getGraphicLocation(me._mapView, e);
            };
            if (me._options.identifyUrl) {
                var _vecClickHandleKeys = Object.keys(view._vecClickHandleObj);
                var _vecClickHandleLen = _vecClickHandleKeys.length;
                me._mapUtils._currentClickLayer = me.id;
                me._mapUtils.identifyTask(me._mapView, me._options.identifyUrl, {
                    geometry: pt,
                    callback: function(evt) {
                        if (!view._clickHandleCount) {
                            view._clickHandleCount = 1;
                        } else {
                            view._clickHandleCount++;
                        };

                        evt.mapView = me._mapView;

                        view._vecClickEvtObj[me.id] = evt;

                        if (view._clickHandleCount == _vecClickHandleLen) {
                            view._clickHandleCount = 0;
                            var _clickLayerNames = Object.keys(view._vecClickEvtObj);
                            for (let i = _clickLayerNames.length - 1; i >= 0; i--) {
                                var _layerName = _vecClickHandleKeys[i];
                                var e = view._vecClickEvtObj[_layerName];
                                if (e && e.features && e.features.length > 0) {
                                    var _layer = me._mapUtils.getLayer(me._mapView, _layerName);
                                    if (_layer && _layer.visible) {
                                        _layer._options.onClick(e);
                                        break;
                                    };
                                };
                            };
                            view._vecClickEvtObj = {};
                        }
                    },
                    ...me._options.identifyOptions
                });
            } else if (me._options.queryUrl) {
                me._mapUtils.queryTask(me._mapView, me._options.queryUrl, {
                    geometry: pt,
                    callback: function(evt) {
                        evt.getLocation = e.getLocation;
                        me._options.onClick(evt);
                    },
                    ...me._options.queryOptions
                })
            };
        },
        getGraphicLocation: function(mapView, esriEvent) {
            var geometry = esriEvent.mapPoint;
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        }
    });
    return vectorTileLayer;
});